package xyz.bali16.module.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.exception.BusinessException;
import xyz.bali16.application.core.utils.TimeUtil;
import xyz.bali16.module.sign.entity.SignAttendance;
import xyz.bali16.module.sign.entity.SignEvent;
import xyz.bali16.module.sign.mapper.SignAttendanceMapper;
import xyz.bali16.module.sign.mapper.SignEventMapper;
import xyz.bali16.module.sign.model.*;
import xyz.bali16.module.sign.service.SignAttendanceService;

import java.time.LocalDateTime;

import static xyz.bali16.application.core.utils.TimeUtil.FORMAT_SECONDS;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class SignAttendanceServiceImpl extends ServiceImpl<SignAttendanceMapper, SignAttendance> implements SignAttendanceService {
    private final SignAttendanceMapper signAttendanceMapper;
    private final SignEventMapper signEventMapper;

    public SignAttendanceServiceImpl(SignAttendanceMapper signAttendanceMapper, SignEventMapper signEventMapper) {
        this.signAttendanceMapper = signAttendanceMapper;
        this.signEventMapper = signEventMapper;
    }

    @Override
    public IPage<SignAttendance> getPage(SignAttendancePageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getUserId(), param.getEventId());
    }

    @Override
    public void addSignAttendance(SignAttendanceAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "SignPointAddParam 参数不能为为空");
        SignAttendance sign = new SignAttendance();
        BeanUtils.copyProperties(param, sign);
        // 判断签到是否在指定的签到事件有效时间内
        SignEvent signEvent = signEventMapper.selectById(sign.getEventId());
        long startTime = TimeUtil.dateToStamp(signEvent.getStartTime(),FORMAT_SECONDS);
        long endTime = TimeUtil.dateToStamp(signEvent.getEndTime(),FORMAT_SECONDS);
        long curTime = System.currentTimeMillis();
        if (curTime >= startTime && curTime <= endTime) {
            sign.setCheckinTime(TimeUtil.getNowTimeToSeconds());
            sign.setCheckinDate(TimeUtil.getNowTimeToDay());
            System.out.println(sign);
            save(sign);
        }else {
            throw new BusinessException("不在签到时间中");
        }
    }
    @Override
    public boolean has(SignAttendanceAddParam param) {
        Preconditions.checkNotNull(param, "SignPointAddParam 参数不能为为空");
        Wrapper wrapper = Wrappers.<SignAttendance>lambdaQuery().eq(SignAttendance::getCheckinDate,TimeUtil.getNowTimeToDay());
        SignAttendance sign = signAttendanceMapper.selectOne(wrapper);
        if(sign!=null){
            return true;
        }
        return false;
    }

    @Override
    public void updateSignAttendance(SignAttendanceUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "SignPointAddParam 参数不能为为空");
        SignAttendance sign = new SignAttendance();
        BeanUtils.copyProperties(param, sign);
        sign.setCheckinTime(TimeUtil.getNowTimeToSeconds());
        sign.setCheckinDate(TimeUtil.getNowTimeToDay());
        updateById(sign);
    }

    @Override
    public void delete(String signId) throws Exception {
        if (signId == null) {
            return;
        }
        removeById(signId);

    }
}
